package com.syh.jn.scm.service.impl.upstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.base.UpBusinessEntityInvoiceInfo;
import com.syh.jn.scm.domain.upstream.repository.base.UpInvoiceInfoRepository;
import com.syh.jn.scm.dto.upstream.base.UpInvoiceInfoMto;
import com.syh.jn.scm.dto.upstream.base.UpInvoiceInfoSto;
import com.syh.jn.scm.dto.upstream.base.UpInvoiceInfoVo;
import com.syh.jn.scm.service.api.upstream.base.UpBusinessEntityInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开票信息管理
 */
@Service
@Transactional
public class UpBusinessEntityInvoiceInfoServiceImpl extends AbstractServiceImpl<UpInvoiceInfoSto, UpInvoiceInfoMto, UpInvoiceInfoVo, UpBusinessEntityInvoiceInfo, Long> implements UpBusinessEntityInvoiceInfoService {
  @Autowired
  private UpInvoiceInfoRepository upBusinessEntityInvoiceInfoRepositoryImpl;

  @Override
  protected AggregateRepository<UpBusinessEntityInvoiceInfo, Long> getAggregateRepository() {
    return upBusinessEntityInvoiceInfoRepositoryImpl;
  }

  /**
   * 保存对象
   * @param sto
   * @return
   */
  @Override
  public Long save(UpInvoiceInfoSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    UpBusinessEntityInvoiceInfo basicInfo = UpBusinessEntityInvoiceInfo.create(sto);
    return upBusinessEntityInvoiceInfoRepositoryImpl.add(basicInfo);
  }

  /**
   * 修改对象
   * @param mto
   */
  @Override
  public void update(UpInvoiceInfoMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    UpBusinessEntityInvoiceInfo basicInfo = upBusinessEntityInvoiceInfoRepositoryImpl.getById(mto.getId());
    ExtAssert.notNull(basicInfo, "对象为空");

    basicInfo.modify(mto);
    upBusinessEntityInvoiceInfoRepositoryImpl.update(basicInfo);
  }

  /**
   * 删除对象
   * @param id
   */
  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");

    UpBusinessEntityInvoiceInfo basicInfo = upBusinessEntityInvoiceInfoRepositoryImpl.getById(id);
    ExtAssert.notNull(basicInfo, "对象为空");

    basicInfo.destroy();
    upBusinessEntityInvoiceInfoRepositoryImpl.remove(basicInfo);
  }
}
